from django.shortcuts import render
from django.shortcuts import HttpResponse
from django.views.decorators.csrf import csrf_exempt
# Create your views here.
import json
from assets import models
from assets import asset_handler
from django.shortcuts import get_object_or_404
from .models import Manager
#
#
# # Create your views here.
# def index_view(request):
#     # 根据不同的请求方式处理不同的业务需求
#     if request.method == 'GET':
#         return render(request, 'register.html')
#     else:
#         # 1.接收请求参数
#         name = request.POST.get('name', '')
#         pwd = request.POST.get('pwd', '')
#
#         # 2.非空判断
#         if name and pwd:
#             # 3.创建模型对象
#             user = User(name=name, pwd=pwd)
#             # 4.插入数据库
#             user.save()
#         # 5.页面响应
#             return HttpResponse('注册成功！')
#     return HttpResponse('注册失败！')
#
#
def login(request):
    if request.method == 'GET':
        return render(request,'assets/login.html')
    else:
        # 1.获取请求参数
        name = request.POST.get('name')
        pwd = request.POST.get('pwd')

        # 2.查询数据库
        if name and pwd:
            c = Manager.objects.filter(name=name, pwd=pwd).count()
            if c == 1:
                return HttpResponse('登录成功！')
        # 3.判断是否登录成功
        return HttpResponse('登录失败！')

def index(request):
    """
    资产总表视图
    :param request:
    :return:
    """

    assets = models.Asset.objects.all()
    return render(request, 'assets/index.html', locals())


def dashboard(request):
    if request.method == 'GET':
        return render(request, 'login.html')
    else:
        # 1.获取请求参数
        name = request.POST.get('name')
        pwd = request.POST.get('pwd')
        # 2.查询数据库
        if name and pwd:
            c = Manager.objects.filter(name=name, pwd=pwd).count()
            if c == 1:
                total = models.Asset.objects.count()
                upline = models.Asset.objects.filter(status=0).count()
                offline = models.Asset.objects.filter(status=1).count()
                unknown = models.Asset.objects.filter(status=2).count()
                breakdown = models.Asset.objects.filter(status=3).count()
                backup = models.Asset.objects.filter(status=4).count()

                up_rate = round(upline/total*100)
                o_rate = round(offline / total * 100)
                un_rate = round(unknown / total * 100)
                bd_rate = round(breakdown / total * 100)
                bu_rate = round(backup / total * 100)

                server_number = models.Server.objects.count()
                networkdevice_number = models.NetworkDevice.objects.count()

                return render(request, 'assets/dashboard.html', locals())
            else:
                return HttpResponse('账号或密码错误!')



def detail(request, asset_id):
    """
    以显示服务器类型资产详细为例，安全设备、存储设备、网络设备等参照此例。
    :param request:
    :param asset_id:
    :return:
    """

    asset = get_object_or_404(models.Asset, id=asset_id)

    return render(request, 'assets/detail.html', locals())


@csrf_exempt
def report(request):
    if request.method == 'POST':
        asset_data = request.POST.get('asset_data')
        print(asset_data)
        data = json.loads(asset_data)
        if not data:
            return HttpResponse('没有数据！')
        if not issubclass(dict, type(data)):
            return HttpResponse('数据必须为字典格式！')
        # 你的检测代码

        sn = data.get('sn', None)
        if sn:
            asset_obj = models.Asset.objects.filter(sn=sn)  # [obj]

            if asset_obj:
                update_asset = asset_handler.UpdateAsset(request, asset_obj[0], data)
                return HttpResponse('资产数据已经更新。')
            else:
                obj = asset_handler.NewAsset(request, data)
                response = obj.add_to_new_assets_zone()
                return HttpResponse(response)
        else:
            return HttpResponse('没有资产sn，请检查数据内容！')

    return HttpResponse('200 ok')
